package com.mg.yygh.hosp.controller.api;

import com.mg.yygh.common.exception.YyghException;
import com.mg.yygh.common.helper.HttpRequestHelper;
import com.mg.yygh.common.result.Result;
import com.mg.yygh.common.result.ResultCodeEnum;
import com.mg.yygh.common.utils.MD5;
import com.mg.yygh.hosp.repository.ScheduleRepository;
import com.mg.yygh.hosp.service.DepartmentService;
import com.mg.yygh.hosp.service.HospitalService;
import com.mg.yygh.hosp.service.HospitalSetService;
import com.mg.yygh.hosp.service.ScheduleService;
import com.mg.yygh.hosp.utils.SignVerify;
import com.mg.yygh.model.hosp.Department;
import com.mg.yygh.model.hosp.Hospital;
import com.mg.yygh.model.hosp.Schedule;
import com.mg.yygh.vo.hosp.DepartmentQueryVo;
import com.mg.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author M_Dragon
 * @date 2022/8/30/030 10:28
 */
@Api(tags = "医院管理api接口")
@RequestMapping("/api/hosp")
@RestController
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService setService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;



    // 上传“医院”接口
    // 平台接收 医院系统上传的数据 保存到 mongodb数据库
    @ApiOperation("上传“医院”接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 通过request获取参数map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 数字签名 判断 是否一致  switchMap 中sign 经过MD5加密 ，需要同样加密再做判断
        String key = (String) map.get("sign");

        String hoscode = (String) map.get("hoscode");

        String signKey = setService.getSignKeyByCode(hoscode);

        String MdSignkey = MD5.encrypt(signKey);

        if (!key.equals(MdSignkey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 传输过程中（从json读取） "+" 转换成了 " " , 需要转换回来
        String logoData = (String) map.get("logoData");
        String all = logoData.replaceAll(" ", "+");
        map.put("logoData",all);

        hospitalService.save(map);

        return Result.ok();
    }

    @ApiOperation("查询“医院”接口")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 通过request获取参数map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 数字签名 判断 是否一致  switchMap 中sign 经过MD5加密 ，需要同样加密再做判断
        String key = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        String signKey = setService.getSignKeyByCode(hoscode);
        String MdSignkey = MD5.encrypt(signKey);
        if (!key.equals(MdSignkey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }



        Hospital hospital = hospitalService.getHospitalByCode(hoscode);

        return Result.ok(hospital);
    }
    // 上传“科室”接口
    @ApiOperation("上传“科室”接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 通过request获取参数map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String key = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        String signKey = setService.getSignKeyByCode(hoscode);
        String MdSignkey = MD5.encrypt(signKey);
        if (!key.equals(MdSignkey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(map);
        return Result.ok();
    }

    @ApiOperation("查询“科室”接口")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) map.get("hoscode");

        int page = StringUtils.isEmpty(map.get("page"))? 1:Integer.parseInt((String)map.get("page"));;
        int limit = StringUtils.isEmpty(map.get("limit"))? 1:Integer.parseInt((String)map.get("limit"));;

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> departments = departmentService.getPage(page,limit,departmentQueryVo);
        return Result.ok(departments);
    }

    @ApiOperation("删除“科室”接口")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }


    // 上传“排班”接口
    @ApiOperation("上传“排班”接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 通过request获取参数map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String key = (String) map.get("sign");
        System.out.println("1======"+key);
        String hoscode = (String) map.get("hoscode");
        String signKey = setService.getSignKeyByCode(hoscode);
        System.out.println("2======"+signKey);
        String MdSignkey = MD5.encrypt(signKey);
        System.out.println("3======"+MdSignkey);
        if (!key.equals(MdSignkey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(map);
        return Result.ok();
    }

    @ApiOperation("查询“排班”接口")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) map.get("hoscode");

        int page = StringUtils.isEmpty(map.get("page"))? 1:Integer.parseInt((String)map.get("page"));;
        int limit = StringUtils.isEmpty(map.get("limit"))? 1:Integer.parseInt((String)map.get("limit"));;

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        Page<Schedule> schedules = scheduleService.getPage(page,limit,scheduleQueryVo);
        return Result.ok(schedules);
    }

    @ApiOperation("删除“排班”接口")
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 通过工具类将字符串数组转为object
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

}
